package com.yuwubao.uav.tool.service.impl;

import com.yuwubao.framework.bean.PageData;
import com.yuwubao.uav.tool.mapper.AdviceMapper;
import com.yuwubao.uav.tool.service.AdviceService;
import com.yuwubao.uav.tool.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@SuppressWarnings("all")
@Service
@EnableTransactionManagement
public class AdviceServiceImpl implements AdviceService {

    private static final Logger logger = LogManager.getLogger(AdviceServiceImpl.class.getName());

    @Resource
    private AdviceMapper adviceMapper;

    @Transactional
    @Override
    public void deleteAdvice(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("id",idArr);
                    adviceMapper.deleteAdvice(pageData);
                    adviceMapper.deleteAdviceAttach(pageData);
                }
            }



        }catch (Exception e) {
            logger.debug("",e);
        }
    }
    @Transactional
    @Override
    public void disableOrEnableAdvice(PageData pageData) throws Exception {
        try{
            String ids = pageData.get("ids") == null ? "" : pageData.getString("ids");

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length>0) {
                    pageData.put("id",idArr);
                    adviceMapper.disableOrEnableAdvice(pageData);
                }
            }



        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Transactional
    @Override
    public void updateAdvice(PageData pageData) throws Exception {
        try{
            adviceMapper.updateAdvice(pageData);
            adviceMapper.deleteAdviceAttach(pageData);
            String attachs = pageData.get("attachs") == null ? "" : pageData.getString("attachs");
            String attachUrls = pageData.get("attachUrls") == null ? "" : pageData.getString("attachUrls");

            String[] attachUrlArr = null;
            if (StringUtil.isNotNvlOrBlank(attachUrls)) {
                attachUrlArr = attachUrls.split(",");
            }


            if (StringUtil.isNotNvlOrBlank(attachs)) {
                String[] attachArr = attachs.split(",");
                if (attachUrlArr!=null && attachArr != null && attachArr.length>0) {
                    if (attachUrlArr.length == attachArr.length) {
                        for (int i =0;i<attachArr.length;i++) {
                            pageData.put("attach", attachArr[i]);
                            pageData.put("url",attachUrlArr[i]);
                            adviceMapper.addAdviceAttach(pageData);
                        }
                    }

                }
            }

        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Transactional
    @Override
    public void addAdvice(PageData pageData) throws Exception {
        try{
            adviceMapper.addAdvice(pageData);

            String attachs = pageData.get("attachs") == null ? "" : pageData.getString("attachs");
            String attachUrls = pageData.get("attachUrls") == null ? "" : pageData.getString("attachUrls");

            String[] attachUrlArr = null;
            if (StringUtil.isNotNvlOrBlank(attachUrls)) {
                attachUrlArr = attachUrls.split(",");
            }

            if (StringUtil.isNotNvlOrBlank(attachs)) {
                String[] attachArr = attachs.split(",");
                if (attachUrlArr!=null && attachArr != null && attachArr.length>0) {
                    if (attachUrlArr.length == attachArr.length) {
                        for (int i =0;i<attachArr.length;i++) {
                            pageData.put("attach", attachArr[i]);
                            pageData.put("url",attachUrlArr[i]);
                            adviceMapper.addAdviceAttach(pageData);
                        }
                    }
                }
            }

        }catch (Exception e) {
            logger.debug("",e);
        }
    }

    @Transactional
    @Override
    public List<PageData> listAdvice(PageData pageData) throws Exception {
        try{
            List<PageData> list = adviceMapper.listAdvice(pageData);
            if (list != null && list.size()>0) {
                for (PageData pd : list) {
                    pd.put("attach_list",adviceMapper.listAdviceAttach(pd));
                }
            }
            return list;
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listAdviceCount(PageData pageData) throws Exception {
        try{
            return adviceMapper.listAdviceCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

    @Override
    public List<PageData> listAdviceEnrollUser(PageData pageData) throws Exception {
        try{
            return adviceMapper.listAdviceEnrollUser(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return null;
    }
    @Override
    public int listAdviceEnrollUserCount(PageData pageData) throws Exception {
        try{
            return adviceMapper.listAdviceEnrollUserCount(pageData);
        }catch (Exception e) {
            logger.debug("",e);
        }
        return 0;
    }

}
